package com.lcf.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lcf.system.dto.user.LoginDTO;
import com.lcf.system.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表(User)表控制层
 *
 * @author lcf
 * @since 2025-02-19 13:27:49
 */
@Tag(name = "登陆")
@RestController
@RequestMapping("auth")
public class AuthController {
    @Resource
    private AuthService authService;

    /**
     * 小程序授权
     */
    @PostMapping("/login")
    @Operation(summary = "小程序授权")
    public String miniAppLogin(String code) {
        authService.miniAppLogin(code);

        return StpUtil.getTokenInfo().getTokenValue();
    }

    /**
     * 账号密码登录
     */
    @PostMapping("/admin/login")
    @Operation(summary = "账号密码登录")
    public String login(@RequestBody @Validated LoginDTO loginDTO) {
        authService.login(loginDTO);

        return StpUtil.getTokenInfo().getTokenValue();
    }

    @GetMapping("test")
    public void test() {
        StpUtil.login("admin");
    }
}

